﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Krystalware.SlickUpload.Web.Controls;
using Krystalware.SlickUpload;

namespace BitPortal.Modules.UploadFile
{
    public partial class UploadFile : Controls.BaseUserControl
    {
        public ICollection<UploadedFile> UploadedFiles { set; get; }
        protected void updateButton_Click(object sender, EventArgs e)
        {
            updateLabel.Text = DateTime.Now.ToLongTimeString();
        }
        protected void slickUpload_UploadComplete(object sender, UploadSessionEventArgs e)
        {
            uploadResultPanel.Visible = true;
            uploadPanel.Visible = false;

            if (e.UploadSession != null && e.UploadSession.State == UploadState.Complete)
            {
                if (e.UploadSession.UploadedFiles.Count > 0)
                {
                    this.UploadedFiles = e.UploadSession.UploadedFiles;
                    resultsRepeater.DataSource = e.UploadSession.UploadedFiles;
                    resultsRepeater.DataBind();
                }
            }
        }
        protected void newUploadButton_Click(object sender, EventArgs e)
        {
            uploadResultPanel.Visible = false;
            uploadPanel.Visible = true;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            slickUpload.MaxFiles = 3;
            slickUpload.MaxFileSize = 2048;
            slickUpload.ValidExtensions = ".jpg,.png,.gif,.rar,.zip,.doc,.docx,.pdf,.xls";
            slickUpload.InvalidExtensionMessage = "Định dạng file cho phép:.jpg,.png,.gif,.rar,.zip,.doc,.docx,.pdf,.xls <br/>";
            slickUpload.InvalidFileSizeMessage = "Kích thước file tối đa: 2 Mb";

            requiredFilesValidator.Enabled = true;

            summaryValidator.Text = "Thông báo";
            summaryValidator.Enabled = true;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            // Ensure ASP.NET AJAX controls initialize and render properly
            this.Page.ClientScript.RegisterForEventValidation(newUploadButton.UniqueID);
            base.Render(writer);
        }

    }
}